import 'package:flutter/material.dart';
import 'package:flutter_app/page/registerpage.dart';
import 'package:flutter_app/utils/string_utils.dart';
import 'package:flutter_app/utils/toast.dart';
import 'package:flutter_app/view/appbar.dart';
import 'package:flutter_app/view/login_button.dart';
import 'package:flutter_app/view/login_input.dart';
import 'package:flutter_app/view/logineffect.dart';

class LoginPage extends StatefulWidget {
  final VoidCallback onJumpToRegistion;
  final VoidCallback success;

  const LoginPage({Key key, this.onJumpToRegistion, this.success})
      : super(key: key);

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  bool protect = false;
  bool loginEnable = false;
  String username;
  String passwrod;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar(() {
          widget.onJumpToRegistion();
      }, "登录", "注册", centertitle: true),
      body: Container(
        color: Colors.white,
        child: ListView(
          children: [
            LoginEffect(protect: protect),
            LoginInput(
              "用户名",
              "请输入用户名",
              onChanged: (text) {
                username = text;
                checkInput();
              },
              focuChanged: (focus) {
                setState(() {
                  protect = !focus;
                });
              },
            ),
            LoginInput(
              "密码",
              "请输入密码",
              lineStretch: true,
              obscureText: true,
              onChanged: (text) {
                passwrod = text;
                checkInput();
              },
              focuChanged: (focus) {
                setState(() {
                  protect = focus;
                });
              },
            ),
            Padding(
              padding: const EdgeInsets.only(left: 20, right: 20, top: 20),
              child: LoginButton(
                "登录",
                enable: loginEnable,
                onPressed: _registerButton,
              ),
            )
          ],
        ),
      ),
    );
  }

  void checkInput() {
    bool enable;
    if (isNotEmpty(username) && isNotEmpty(passwrod)) {
      enable = true;
    } else {
      enable = false;
    }
    setState(() {
      loginEnable = enable;
    });
  }

  ///注册按钮
  _registerButton() {
    return InkWell(
      onTap: () {
        if (loginEnable) {
          send();
        } else {
          showToast("还有参数没填");
        }
      },
      child: const Center(
        child: Text("登录"),
      ),
    );
  }

  void send() {
    if (widget.success != null) {
      widget.success();
    }
  }
}
